package org.easy.core.util;

public class RGBAtoRGBUtil {

    public static int[] convertRGBAtoRGB(int[] rgba, double alpha) {
        // 假设背景色为白色，RGB值分别为255
        int backgroundColorRed = 255;
        int backgroundColorGreen = 255;
        int backgroundColorBlue = 255;

        // 归一化处理，将传入的RGBA值中的RGB通道值转换到0-1范围（这里alpha已经是0-1范围了）
        double normalizedRed = (double) rgba[0] / 255;
        double normalizedGreen = (double) rgba[1] / 255;
        double normalizedBlue = (double) rgba[2] / 255;

        // 根据混合公式计算新的RGB值
        double newRed = backgroundColorRed * (1 - alpha) + normalizedRed * alpha;
        double newGreen = backgroundColorGreen * (1 - alpha) + normalizedGreen * alpha;
        double newBlue = backgroundColorBlue * (1 - alpha) + normalizedBlue * alpha;

        // 将计算后的0-1范围的RGB值转换回0-255范围并取整
        int finalRed = (int) Math.round(newRed * 255);
        int finalGreen = (int) Math.round(newGreen * 255);
        int finalBlue = (int) Math.round(newBlue * 255);

        return new int[]{finalRed, finalGreen, finalBlue};
    }


    public static void main(String[] args) {
        int[] rgba = {234, 233, 233};
        double alpha = 0.7;
        int[] result = convertRGBAtoRGB(rgba, alpha);
        System.out.println("转换后的RGB值为: " + result[0] + ", " + result[1] + ", " + result[2]);
    }
}